package com.code.utils;

import com.code.enums.TableFieldValid;
import com.code.entity.metadata.Column;
import com.code.entity.metadata.Table;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class DataProcessUtils {


    /**
     * 获取jdbc type
     *
     * @param value
     * @return
     */
    public static String getJdbcType(String value) {
        if (value == null || value.isEmpty()) {
            return "";
        }
        value = value.toUpperCase();
        switch (value) {
            case "INT":
                return "INTEGER";
            case "TIMESTAMP":
            case "DATETIME":
            case "DATE":
                return "TIMESTAMP";
            case "TEXT":
            case "VARCHAR2":
                return "VARCHAR";
        }
        return value;
    }

    /**
     * 获取java type
     *
     * @param value
     * @return
     */
    public static String getJavaType(String value) {
        if (value == null || value.isEmpty()) {
            return "";
        }
        value = value.toUpperCase();
        switch (value) {
            case "CHAR":
            case "VARCHAR":
            case "VARCHAR2":
            case "LONGVARCHAR":
            case "TEXT":
                return "String";
            case "BIT":
            case "BOOLEAN":
                return "Boolean";
            case "INT":
            case "INTEGER":
            case "TINYINT":
                return "Integer";
            case "BIGINT":
                return "Long";
            case "DECIMAL":
            case "NUMBER":
            case "NUMERIC":
                return "BigDecimal";
            case "REAL":
                return "Float";
            case "FLOAT":
            case "DOUBLE":
                return "Double";
            case "DATE":
            case "TIMESTAMP":
            case "DATETIME":
                return "Date";
        }
        return "";
    }


    /**
     * 校验表格基础字段，基础字段定义在：TableFieldValid
     *
     * @param tableMappings
     */
    public static void validTableField(Map<String, Table> tableMappings) {
        Set<String> tables = tableMappings.keySet();
        String tableValid = String.join(",", tables);
        log.info("开始校验表格基础字段，校验表格项：{}", tableValid);
        for (String tableName : tableMappings.keySet()) {
            Table table = tableMappings.get(tableName);
            List<Column> columns = table.getColumns();
            Map<String, String> columnsMap = columns.stream()
                    .collect(Collectors.toMap(Column::getName, Column::getJdbcType));
            TableFieldValid[] values = TableFieldValid.values();
            for (TableFieldValid ele : values) {
                String key = ele.getKey();
                String columnsKey = columnsMap.get(key);
                String type = ele.getType();
                //if (!(Objects.nonNull(columnsKey) && columnsKey.equals(type))) {
                //    throw new RuntimeException("表格：" + tableName + "，缺少字段：" +
                //            key + "，或字段数据类型错误，要求数据类型：" + type);
                //}
            }
        }
        log.info("完成表格基础字段校验，校验表格项：{}", tableValid);
    }

}
